package com.meisiabc.app.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.meisiabc.app.service.OrderService;
import com.meisiabc.app.web.auth.AssertAuth;
import com.meisiabc.app.web.auth.SecurityContextHolder;
import com.meisiabc.support.Result;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @AssertAuth
    @GetMapping("/list")
    public Result list(Integer is_pay) {
        String mobile = SecurityContextHolder.getContext().getMobile();
        return Result.ok(orderService.getOrderList(mobile, is_pay));
    }

    @AssertAuth
    @PostMapping("/prepay")
    public Result prepay(@RequestBody JSONObject loginForm) {
        Integer material = loginForm.getInteger("material");
        Integer hoursEnrolled = loginForm.getInteger("hoursEnrolled");
        BigDecimal money = loginForm.getBigDecimal("amount_money");


        String mobile = SecurityContextHolder.getContext().getMobile();
        String openId = SecurityContextHolder.getContext().getOpenid();

        return Result.ok(orderService.prepay(mobile, openId, material, hoursEnrolled, money, DateTime.now()));
    }

    @RequestMapping("/payCallback")
    public String payCallback(@RequestBody String body) {
        orderService.payCallback(body);
        return orderService.payCallbackSuccess();
    }
}
